<?php
	class ImageManageAction extends Action{
		//通栏首页
		public function advert(){
			$image=M('Image');						//实例化图片表
			if($_POST['numPerPage']){
				$pagesize = $this->_post('numPerPage');
				$_GET['page'] = $this->_post('pageNum');//自定义翻页类需要 GET （当前页）信息
			}else{
				$pagesize=20;
			}
			$totalCount=$image->where('Status=0')->count();			//获取总条数
			$this->assign('totalCount',$totalCount);				//把总数发给模板
			import('@.Action.Page');
			$page = new Page($totalCount,$pagesize);				//实例化分页类
			$pageinfo = $page->getPageInfo();						//获取分页
			$this->assign('currentPage',$pageinfo['page']);			//分配当前页数
			$this->assign('pagesize',$pagesize);					//分配一页显示的条数
			$arr=$image->field('Id,Name,Link,Tips')->where('Status=0')->limit($pageinfo['offset'],$pageinfo['num'])->select();	//获取图片信息
			for($i=0;$i<count($arr);$i++){
				$arr[$i]['Name']='./UpFile/Img/Advert/'.$arr[$i]['Name'];		//组合图片路径
			}
			$this->assign('arr',$arr);
			$this->display();
		}
		//修改通栏页
		public function addadvert(){		
			$this->display();
		}
		//图片首页
		public function image(){
			$image=M('Image');
			if($_POST['numPerPage']){
				$pagesize = $this->_post('numPerPage');
				$_GET['page'] = $this->_post('pageNum');//自定义翻页类需要 GET （当前页）信息
			}else{
				$pagesize=20;
			}
			$totalCount=$image->where('Status=1')->count();
			$this->assign('totalCount',$totalCount);				//把总数发给模板
			import('@.Action.Page');
			$page = new Page($totalCount,$pagesize);				//实例化分页类
			$pageinfo = $page->getPageInfo();						//获取分页
			$this->assign('currentPage',$pageinfo['page']);			//分配当前页数
			$this->assign('pagesize',$pagesize);					//分配一页显示的条数
			$arr=$image->field('Id,Name,Link,Title,Instruction')->where('Status=1')->limit($pageinfo['offset'],$pageinfo['num'])->select();
			
			for($i=0;$i<count($arr);$i++){
				$arr[$i]['Name']='./UpFile/Img/Image/'.$arr[$i]['Name'];
			}
			$this->assign('arr',$arr);
			$this->display();
		}
		//添加首页图片页
		public function addimage(){
			$this->display();
		}
		//修改图片和广告
		public function upload(){
			$image=M('image');
			$type=$this->_post('type');						//获取类型
			import('ORG.Net.UploadFile');
			$upload = new UploadFile();									// 实例化上传类
			$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');	// 设置附件上传类型
			$upload->maxSize  = 31457280;								// 设置附件上传大小
			$upload->savePath = './UpFile/Img/test/';					// 设置附件上传目录
			if(!$upload->upload()){										// 上传错误提示错误信息
				$this->error_ajax($upload->getErrorMsg());
			}else{														// 上传成功 获取上传文件信息
				$info =  $upload->getUploadFileInfo();
				$data['Name']=$info[0]['savename'];						//获取图片名
				$opath="./UpFile/Img/test/".$data['Name'];				//获取旧地址
				$npath="./UpFile/Img/Image";							//获取新地址
				$npath1="./UpFile/Img/Advert";							//获取新地址
			}
			$data['Link']=$this->_post('Link');							//获取连接
			if($type==1){												//是广告执行
				$data['Tips']=$this->_post('Tips');						//获取Tips
				doresize(200,$opath,$npath1,$data['Name']);				//移动图片
			}else{														//是图片执行
				$data['Status']=1;										//图片为1
				$data['Title']=$this->_post('Title');					//获取标题
				$data['Instruction']=$this->_post('Instruction');		//获取说明
				doresize(500,$opath,$npath,$data['Name'],385);			//移动图片缩放
			}
			unlink('./UpFile/Img/test/'.$data['Name']);					//删除原图片
			if($image->add($data)){										//添加
				$this->success_ajax('添加成功');
			}else{
				$this->error_ajax('添加失败');
			}
		}
		public function delete(){
			$id=$this->_get('uid');					//获取id
			$image=M('image');						//实例化图片表
			if($image->where('Id='.$id)->delete()){		//删除
				$this->success_ajax('删除成功');
			}else{
				$this->error_ajax('删除失败');
			}		
		}
	}
